package com.hu.green.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hu.green.commen.R;
import com.hu.green.entity.Php;
import com.hu.green.service.PhpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@CrossOrigin(value = "*")
@RequestMapping("/php")
public class PhpController {

    @Autowired
    private PhpService phpService;

    /**
     * 更新数据库
     */
    @GetMapping("/update")
    public void update(){
        LambdaQueryWrapper<Php> phpLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Php> phps = phpService.list(phpLambdaQueryWrapper);
        for (Php php : phps) {
            LambdaQueryWrapper<Php> phpLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            phpLambdaQueryWrapper1.eq(Php::getId, php.getId());

            String deleteString = new String();

            for (int i = 1; i < php.getImage().length()-1; i++) {
                if (php.getImage().charAt(i) != '"') {
                    deleteString += php.getImage().charAt(i);
                }
            }
            php.setImage(deleteString);
            // 更新数据
            phpService.update(php, phpLambdaQueryWrapper1);
        }
    }

    /**
     * 发帖子
     * @param php
     * @return
     */
    @PostMapping("/post")
    public R postPhp(@RequestBody Php php){
        // 1. 用户点击发送帖子后信息计入数据库并生成唯一id且获取用户信息
        List<String> img = php.getImg();
        String image = JSON.toJSONString(img);

        // 对字符串image进行处理，去掉双引号和方括号
        String deleteString = new String();

        for (int i = 1; i < image.length()-1; i++) {
            if (image.charAt(i) != '"') {
                deleteString += image.charAt(i);
            }
        }

        php.setImage(deleteString);

        phpService.getMessage(php); // 传入用户信息

        phpService.save(php); // 存储数据

        return R.success("发布成功");
    }

    /**
     * 获取帖子信息
     * @return
     */
    @GetMapping("/get")
    public List<Map<String,Map<String,Object>>> get(){

        phpService.upDate(); // 更新帖子的用户信息

        LambdaQueryWrapper<Php> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderBy(true,false,Php::getId);
        List<Php> list = phpService.list(queryWrapper);

        List<Map<String,Map<String,Object>>> listMap = new ArrayList<Map<String,Map<String,Object>>>();

        for (Php php : list){

            Map<String,Map<String,Object>> maplist = new HashMap<String,Map<String,Object>>();

            Map<String,Object> map = new HashMap<String,Object>();
            Map<String,Object> map1 = new HashMap<String,Object>();

            map.put("id",php.getId());
            map.put("title",php.getTitle());
            map.put("text",php.getMessage());


            List listA= Arrays.asList(php.getImage().split(","));

            map.put("imgs",listA);

            map1.put("name",php.getUserName());
            map1.put("img",php.getUrl());

            maplist.put("message",map);
            maplist.put("userinfo",map1);

            listMap.add(maplist);

        }
        return listMap;
    }

}
